import numpy as np

def inMixData2flost(org_array,new_array):
    for index in range(0,len(org_array)):
        item = org_array[index]
        if item !=b'':
            item = item.decode("gb2312")
            item = float(item)
        else:
            item = None
        new_array[index] = item


def defectsCop(daya_array,threshold):
    for index in range(0,len(daya_array)):
        item = daya_array[index]
        if item >= float(threshold):
            item = None
        daya_array[index] = item

(humidty_str,
 gas_str,
 co_str) = np.loadtxt(
    "ug_detect.csv",
    dtype=bytes,
    delimiter=",",
    skiprows=1,
    usecols=(2,3,4),
    unpack=True
)
def bisec(dataArray):
    for index in range(0,len(dataArray)):
        if np.isnan(dataArray[index]):
            if (index-1)>=0 and (index+1)<len(dataArray):
                dataArray[index] = (dataArray[index-1] + dataArray[index + 1])*0.5

humidty = np.ndarray(len(humidty_str))
inMixData2flost(humidty_str,humidty)
defectsCop(humidty, 200)

gas = np.ndarray(len(gas_str))
inMixData2flost(gas_str,gas)
defectsCop(gas, 100)





co = np.ndarray(len(co_str))
inMixData2flost(co_str,co)
defectsCop(co, 100)


#print("井下的湿度是：\n",humidty)
#print("瓦斯气体浓度：\n",gas)
#print("一氧化碳体浓度：\n",co)

bisec(humidty)
bisec(gas)
bisec(co)

print("井下的湿度是：\n",humidty)
print("瓦斯气体浓度：\n",gas)
print("一氧化碳体浓度：\n",co)

print("保存处理后的湿度数据文件。。。")
np.savetxt("ug_humidity.csv",
           humidty,
           delimiter=",",
           fmt="%.2f")
print("保存处理后的瓦斯浓度数据文件。。。")
np.savetxt("ug_gas.csv",
           gas,
           delimiter=",",
           fmt="%.2f")
print("保存处理后的一氧化碳浓度数据文件。。。")
np.savetxt("ug_co.csv",
           co,
           delimiter=",",
           fmt="%.2f")

print("将这三种井下环境指标的数据写入一个文件当中。。。")
all_data = np.array([humidty,gas,co]).transpose()
np.savetxt(
    "all_data_numpy.csv",
    all_data,
    header="湿度,瓦斯浓度,一氧化碳浓度",
    delimiter=",",
    comments=" ",
    fmt="%.2f"
)

